import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

let time;

let state = {
	count: 0,
	userInfo: {}, //用户信息
	loginState: false, //登录状态


	// 聊天相关
	// 用户列表
	friendList: [],
	// 当前聊天用户
	currUserID: "",
	// 未读消息数
	msgNum: 0,
	// 聊天记录
	msgList: [],
	// 时间戳
	msgTimeStamp: "",
	msgObj: {},
	// TabNum
	tabNum: 0,
	token: '',
	signInfo:{},
	order:[],
	Refresh:false,
	addressMsg:{},
	sendProduct:{},
	paySuccess: {},
	customIDList:[],//选中客户id列表
	shareParams:{},//分享小程序参数
	
	shopCanvas:{},//商品分享参数
	mapCanvas:{},
}

let getters = {
	doneTodos: state => {
		return state.count
	},
	getLoginState: state => {
		let token = uni.getStorageSync('xj_token');
		
		if (token) {
			state.loginState = true
			return true
		}
		return new Promise((resolve, reject) => {
			if (time) clearInterval(time)
			time = setInterval(() => {
				let token = uni.getStorageSync('xj_token')
				if (token) {
					clearInterval(time)
					state.loginState = true
					resolve(true)
				}
			}, 50)
		})
	},

	getToken: state => {
		if (!state.token) {
			state.token = uni.getStorageSync('xj_token');
		}
		return state.token
	},

	getMsgList: state => {
		return state.msgList
	},
	getBarBadge: state => {
		return state.BarBadge
	},
	getAddressMsg: state => {
		return state.addressMsg
	},
	getSendProduct: state => {
		return state.sendProduct
	},
	getPaySuccess: state => {
		return state.paySuccess
	}
}

let mutations = {
	add(state) {
		state.count += 2
	},
	setToken(state, token) {
		state.token = token
	},
	// 设置用户信息
	setUser(state, user) {
		console.log('vuex look');
		state.userInfo = user;
	},
	setSign(state, sign){
		state.signInfo = sign;
	},
	setorder(state,order){
		state.order = order
	},
	// 设置登录状态
	login(state, islogin) {
		console.log(state.loginState, islogin)
		state.loginState = islogin;
	},
	currUserID(state, userId) {
		state.currUserID = userId;
	},
	addMsgList(state, msg) {

		msg.forEach(item => {
			if (item.conversationID != state.currUserID) {
				state.msgNum++;
			}
		});
		let msgArr = msg.sort((a, b) => a.orderKey - b.orderKey);
		state.msgList.push(...msgArr);
		state.msgTimeStamp = new Date().getTime();
	},
	setTabNum(state, num) {
		state.tabNum = num;
		if (num > 0) {
			uni.setTabBarBadge({
				index: 3,
				text: num.toString(),
				complete: () => {},
			});
		} else {
			uni.removeTabBarBadge({ index: 3 });
		}
	},
	setTabNum2(state, groupNum) {
		state.tabNum = groupNum;
		if (groupNum > 0) {
			uni.setTabBarBadge({
				index: 3,
				text: '.',
				complete: () => {},
			});
		} else {
			uni.removeTabBarBadge({ index: 3 });
		}
	},
	changeRefresh(state,isRefresh){
		state.Refresh = isRefresh
	},
	setAddressMsg(state,addressMsg){
		state.addressMsg = addressMsg
	},
	setSendProduct(state,sendProduct){
		state.sendProduct = sendProduct
	},
	setPaySuccess(state, paySuccess) {
		state.paySuccess = paySuccess
	},
	setcustomIDList(state,data){
		state.customIDList = data
	},
	setshareParams(state,data){
		state.shareParams = data
	},
	setShopCanvas(state,data){
		state.shopCanvas = data
	},
	setMapCanvas(state,data){
		state.mapCanvas = data
	}
}

let actions = {
	addCountAction(context) {
		context.commit('add')
	}
}

const store = new Vuex.Store({
	state: state,
	getters: getters,
	mutations: mutations,
	actions: actions
})

export default store